<?xml version="1.0" encoding="UTF-8"?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
	xmlns:f="http://xmlns.jcp.org/jsf/core"
	xmlns:h="http://xmlns.jcp.org/jsf/html"
	xmlns:p="http://primefaces.org/ui"
	xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
	template="/WEB-INF/templates/default.xhtml">
	<f:metadata>	   	
	   	<f:viewParam name="moduleName" value="短信列表"></f:viewParam>
   	</f:metadata>
	<ui:define name="head">
		<h:outputScript name="js/DateFormat.js" />
		<style type="text/css">
			.smsTable{
				width:100%;
				border-collapse: collapse;
				border-color:grey;
    			table-layout: fixed
			}
			.tipInfo{
				text-align: center;
				font-size:18px;
			}
			h2{
				text-align: center;
				margin:10px;
			}
			th{
				background: #ebedf0;
    			border-color: #d9d9d9;
    			border: 1px solid #d6d6d6;
			    font-weight: normal;
			    color: #555;
			    padding: 4px 10px;
			    overflow: hidden;
			    border-width: 1px;
			    border-style: solid;
			}
			td{
				padding: 4px 10px;
			    overflow: hidden;
			    border-width: 1px;
			    border-style: solid;
			    color: #222;
			    border-color: #d9d9d9;
			}
		</style>
		<script type="text/javascript">
			var contextPath = '${request.getContextPath()}';
			var pageSize = 10;
			var totalCount;
			$(document).ready(function() {
				generateTable(1,contextPath);
				generatePage();
			});

			function query(){
				generateTable(1,contextPath);
				generatePage();
			}


			function generateTable(pageNumber, contextPath) {
				var destNumber = document.getElementById('searchForm:destNumber').value;
				var content = document.getElementById('searchForm:content').value;
				
				var url = "";
				if (destNumber == "" &amp;&amp; content == "") {
					
					url = contextPath + "/rest/sms/getSmsList/"+ pageNumber + "/" + pageSize + "/''/'' ";
				} else if(destNumber == "" &amp;&amp; content != "" ) {
					url = contextPath + "/rest/sms/getSmsList/"+ pageNumber + "/" + pageSize + "/''/"+content;
				}else if(destNumber != "" &amp;&amp; content == ''){
					url = contextPath + "/rest/sms/getSmsList/"+ pageNumber + "/" + pageSize + "/"+destNumber+"/'' ";
				}else{
					url = contextPath + "/rest/sms/getSmsList/"+ pageNumber + "/" + pageSize + "/"+destNumber+"/"+content;
					}
				$.ajax({
					type : "get",
					contentType : "application/json; charset=utf-8",
					dataType : "json",
					url : url,
					async : false,
					success : function(data) {
						totalCount = data.totalCount;
						$(".smsTable").find("thead").html("");
						$(".smsTable").find("tbody").html("");
						var json;
						var headerHtml = "";
						var bodyHtml = "";
						headerHtml += "<tr><td>手机号</td><td>内容</td><td>发送时间</td><td>状态</td><td>错误信息</td><td>模版code</td></tr>";
						var smsArr = data.smsList;
						for(var index in smsArr){
							var sms = smsArr[index];
							bodyHtml += '<tr>';
							bodyHtml += '<td title="'+sms.destNumber+'">'+sms.destNumber+'</td>';
							bodyHtml += '<td title="'+sms.content+'">'+sms.content+'</td>';
							var sendDate = new Date(sms.sendTime.time);
							bodyHtml += '<td title="'+sendDate.format('yyyy-MM-dd hh:mm:ss')+'">'+sendDate.format('yyyy-MM-dd hh:mm:ss')+'</td>';
							var status = "";
							if(sms.sendFlag == '0')status = "发送失败";
							if(sms.sendFlag == '1')status = "发送成功";
							bodyHtml += '<td title="'+status+'">'+status+'</td>';
							bodyHtml += '<td title="'+sms.feedbackMsg+'">'+sms.feedbackMsg+'</td>';
							bodyHtml += '<td title="'+sms.templateCode+'">'+sms.templateCode+'</td>';

							bodyHtml += '</tr>';
						}
						$(".smsTable").find("thead").html(headerHtml);
						$(".smsTable").find("tbody").html(bodyHtml);
					},
					timeout : 1000,
					error : function(XMLHttpRequest, textStatus, errorThrown) {
						alert("获取重点列表出错！");
					}
				});
			}

			function generatePage(){
				 $("#page").paging({
					 pageNo:1,
				     totalPage: Math.ceil(totalCount/pageSize),
				     totalSize: totalCount,
				     callback: function(pageNumber) {
				    	 generateTable(pageNumber,contextPath);
				     }
				 })

			}
			
		</script>	
		
	</ui:define>
	<ui:define name="content">
		<h:form id="searchForm">
			<div style="margin-bottom:10px;margin-left:10px">
					<p:outputLabel for="destNumber" value="手机号:"></p:outputLabel>
					<p:inputText id="destNumber" style="margin-left:10px"></p:inputText>
					<p:outputLabel for="content" value="短信内容:" style="margin-left:10px"></p:outputLabel>
					<p:inputText id="content" style="margin-left:10px"></p:inputText>
					<div class="actionButtons">
						<p:commandButton value="查询" 
							onclick="query()"
							styleClass="blue-int"></p:commandButton>
						<p:button value="重置" outcome="SmsList.xhtml"></p:button>
					</div>
					<div class="clearbt"></div>
			</div>
			<p:panel header="短信列表(仅供参考，请以短信平台发送状态为准)">
				
				<table class="smsTable" border="1" >
					<thead>
					</thead>
					<tbody>
					</tbody>
				</table>
				<div id="page" class="page_div"></div>
			</p:panel>

		</h:form>
	</ui:define>
</ui:composition>
